import { Body, Controller, Delete, Get, Param, Post, Put, Query, UsePipes } from '@nestjs/common';
import { DictionaryItem } from 'src/entities/DictionaryItem';
import { DictionaryCategoryModelService } from 'src/model/dictionary-category-model/dictionary-category-model.service';
import { DictionaryItemModelService } from 'src/model/dictionary-item-model/dictionary-item-model.service';
import dtoValidationPipe from 'src/pipe/Dto.pipe';
import { errorReturn, successReturn } from 'src/utils';
import { AddDto, EditDto } from './dto';

@Controller('dictionaryItem')
export class DictionaryItemController {
    constructor(
        private model:DictionaryItemModelService,
        private dictionaryCategoryModelService:DictionaryCategoryModelService,
    ){}

    @Get()
    async item(@Query('categoryId') categoryId:number) {
        return this.model.find({categoryId : categoryId})
    }

    @Get('/:categoryCode/:parentId')
    async itemOfCategory(@Param() params) {
        const { categoryCode, parentId } = params
        const category = await this.dictionaryCategoryModelService.findOne({code : categoryCode}, ['id'])
        if(!category) return []
        return this.model.find({categoryId : category.id, parentId : parentId || 0})
    }

    @Post('add')
    @UsePipes(dtoValidationPipe())
    async add(@Body() dto:AddDto) {
        try {
            if((await this.model.isExistsByWhere({parentId : dto.parentId || 0, categoryId : dto.categoryId, name : dto.name}))) return errorReturn('已存在相同的名称')
            if((await this.model.isExistsByWhere({parentId : dto.parentId || 0, categoryId : dto.categoryId, code : dto.code}))) return errorReturn('已存在相同的编码')
            await this.model.insert(dto)
            return successReturn()
        } catch (error) {
            
        }

        return errorReturn()
    }

    @Put('edit/:id')
    @UsePipes(dtoValidationPipe({skipMissingProperties : true}))
    async edit(@Param('id') id:number, @Body() dto:EditDto) {
        try {
            const item = await this.model.findOne({id : id})
            if(!item) errorReturn()
            let updateData:{[key in keyof DictionaryItem]? : DictionaryItem[key]} = {}
            if(item.name != dto.name) {
                updateData.name = dto.name
            }
            if(item.code != dto.code) {
                updateData.code = dto.code
            }

            if((Object.keys(updateData)).length == 0) return successReturn()

            if(updateData.name && (await this.model.isExistsByWhere({parentId : item.parentId, categoryId : item.categoryId, name : dto.name}))) return errorReturn('已存在相同的名称')
            if(updateData.code && (await this.model.isExistsByWhere({parentId : item.parentId, categoryId : item.categoryId, code : dto.code}))) return errorReturn('已存在相同的编码')

            await this.model.update({id : id}, updateData)

            return successReturn()

        } catch (error) {
            
        }

        return errorReturn()
    }

    @Delete('delete/:id')
    async deleteCategory(@Param('id') id:number) {
        try {
            if((await this.model.isExistsByWhere({parentId : id}))) return errorReturn('该分类存在子分类，不可删除')
            await this.model.delete({id : id})
            return successReturn()
        } catch (error) {
            
        }
        return errorReturn()
    }
}
